/**
 * Created by gs on 2017/1/23.
 */
var form = {
    showData : function(url,formId){
        var form = $("#" + formId);
        var params = this.buildParam(decodeURI(url));
        if(!params){
            return;
        }
        form.find(':input').each(function(index){
            if($(this).is(':file') || $(this).is(':button') || $(this).is(':submit')){
                return;
            }
            var name = $(this).attr('name');
            var type = $(this).attr('type');
            var value = params[name];
            if(!value){
                return;
            }
            if(type == 'text'){
                $(this).val(value);
            }else if(type == 'radio'){
                $('input[name='+name+']').each(function(){
                    if($(this).val() == value){
                        $(this).attr('checked','checked')
                    }
                });
            }else if(type == 'checkbox'){
                $('input[name='+name+']').each(function(){
                    var arr_value = value.split(',');
                    var checkboxValue = $(this).val();
                    for(var i = 0;i < arr_value.length;i ++){
                        if(checkboxValue == arr_value[i]){
                            $(this).attr('checked','checked')
                        }
                    }
                });
            }
        });
        form.find('select').each(function(index){
            var name = $(this).attr('name');
            var value = params[name];
            if(!value){
                return;
            }
            $(this).find('option').each(function(){
                if($(this).val() == value){
                    $(this).attr('selected','selected');
                }
            });
        });
    },
    buildParam : function(url){
        var paramObj = {};
        if(url.indexOf('?') == -1){
            return null;
        }
        var params = url.split('?')[1].split('&');
        for(var i = 0;i < params.length;i ++){
            var param = params[i];
            var paramName = param.split('=')[0];
            var paramValue = param.split('=')[1];
            if(!paramName || paramName == 'pageSize' || paramName == 'pageNum'){
                continue;
            }
            if(paramObj[paramName]){
                paramObj[paramName] = paramObj[paramName] + ',' + paramValue;
            }else{
                paramObj[paramName] = paramValue;
            }
        }
        return paramObj;
    }
}